package com.gao.util;


import javax.naming.OperationNotSupportedException;
import java.util.Arrays;

/**
 * @author gao
 * @date 2023/08/28 10:50:18
 */
public class ArrayList<T> implements List<T> {
    private T[] elements;

    private int size;

    public ArrayList() {
        elements = (T[]) new Object[10];
    }

    public ArrayList(int capacity) {
        elements = (T[]) new Object[capacity];
    }

    @Override
    public boolean add(T obj) {
        ensureCapacity(size + 1);
        elements[size++] = obj;
        return true;
    }

    private void ensureCapacity(int nextIndex) {
        if (nextIndex >= elements.length) {
            Arrays.copyOf(elements, size + size >> 1);
        }
    }

    @Override
    public T set(int index, T val) {
        checkRange(index);
        T oldValue = elements[index];
        elements[index] = val;
        return oldValue;
    }

    private void checkRange(int index) {
        if (index < 0 || index > size - 1) {
            throw new RuntimeException("数组下标越界：" + index);
        }
    }

    @Override
    public boolean del(T val) {
        int index = findIndexByValue(val);
        T delValue = del(index);
        return delValue != null;
    }

    private int findIndexByValue(T val) {
        for (int i = 0; i < elements.length; i++) {
            if (elements[i].equals(val)) {
                return i;
            }
        }
        return -1;
    }

    @Override
    public T del(int index) {
        checkRange(index);
        T oldValue = elements[index];
        for (int i = index; i < size - 1; i++) {
            elements[i] = elements[i + 1];
        }
        elements[size - 1] = null;
        size--;
        return oldValue;
    }

    @Override
    public T get(int index) {
        checkRange(index);
        return elements[index];
    }

    @Override
    public Iterator<T> iterator() {
        return new ArrayListIterator<>();
    }

    @Override
    public void reverse() throws OperationNotSupportedException {
        throw new OperationNotSupportedException();
    }

    @Override
    public void reverseInGroup(int k) throws OperationNotSupportedException {
        throw new OperationNotSupportedException();
    }

    @Override
    public void reverse(int start, int end) throws OperationNotSupportedException {
        throw new OperationNotSupportedException();
    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public boolean isEmpty() {
        return size == 0;
    }

    public class ArrayListIterator<T> implements Iterator<T> {
        private int index;

        @Override
        public boolean hasNext() {
            return index < size;
        }

        @Override
        public T next() {
            return (T) elements[index++];
        }
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        Iterator<T> it = iterator();
        while (it.hasNext()) {
            T next = it.next();
            sb.append(next);
            sb.append(",");
        }
        if (sb.length() > 0) {
            sb.deleteCharAt(sb.length() - 1);
        }
        return sb.toString();
    }
}

